package com.vipboot.platform.system.api.impl;

import cn.hutool.core.lang.Opt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.vipboot.framework.biz.api.auth.ApiUserService;
import com.vipboot.framework.biz.api.auth.domain.model.ApiLoginUser;
import com.vipboot.framework.biz.api.auth.domain.model.ApiRole;
import com.vipboot.framework.biz.api.auth.domain.model.ApiXcxLoginUser;
import com.vipboot.framework.core.util.ObjectUtil;
import com.vipboot.platform.system.crud.dao.SysDeptDao;
import com.vipboot.platform.system.crud.dao.SysRoleDao;
import com.vipboot.platform.system.crud.dao.SysUserDao;
import com.vipboot.platform.system.crud.service.PermissionService;
import com.vipboot.platform.system.domain.entity.SysDept;
import com.vipboot.platform.system.domain.entity.SysRole;
import com.vipboot.platform.system.domain.entity.SysUser;
import com.vipboot.platform.system.domain.entity.SysUserRole;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class ApiUserServiceImpl implements ApiUserService {
    private final PermissionService permissionService;
    private final SysUserDao sysUserDao;
    private final SysDeptDao sysDeptDao;
    private final SysRoleDao sysRoleDao;

    @Override
    public ApiLoginUser getUserInfo(String username) {
        LambdaQueryWrapper<SysUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysUser::getUserName, username);
        SysUser sysUser = sysUserDao.getOne(lqw);
        return buildLoginUser(sysUser);
    }

    @Override
    public ApiLoginUser getUserInfo(Long userId, String tenantId) {
        return null;
    }

    @Override
    public ApiLoginUser getUserInfoByPhonenumber(String phonenumber, String tenantId) {
        return null;
    }

    @Override
    public ApiLoginUser getUserInfoByEmail(String email, String tenantId) {
        return null;
    }

    @Override
    public ApiXcxLoginUser getUserInfoByOpenid(String openid) {
        return null;
    }

    @Override
    public String selectUserNameById(Long userId) {
        return "";
    }

    @Override
    public String selectNicknameById(Long userId) {
        return "";
    }


    /**
     * 构建登录用户
     */
    private ApiLoginUser buildLoginUser(SysUser sysUser) {
//        SysDept sysDept = sysDeptDao.getById(sysUser.getDeptId());
//        List<SysRole> sysRoles = sysRoleDao.listUserRoleByUserId(sysUser.getUserId());

        ApiLoginUser loginUser = new ApiLoginUser();
        loginUser.setTenantId(sysUser.getTenantId());
        loginUser.setUserId(sysUser.getUserId());
        loginUser.setDeptId(sysUser.getDeptId());
        loginUser.setUsername(sysUser.getUserName());
        loginUser.setNickname(sysUser.getNickName());
        loginUser.setPassword(sysUser.getPassword());
        loginUser.setUserType(sysUser.getUserType());

        loginUser.setMenuPermission(permissionService.getMenuPermission(sysUser.getUserId()));
        loginUser.setRolePermission(permissionService.getRolePermission(sysUser.getUserId()));
        if (ObjectUtil.isNotNull(sysUser.getDeptId())) {
            Opt<SysDept> deptOpt = Opt.of(sysUser.getDeptId()).map(sysDeptDao::getById);
            loginUser.setDeptName(deptOpt.map(SysDept::getDeptName).orElse(""));
        }
        loginUser.setRoles(listRoleByUserId(sysUser.getUserId()));
        return loginUser;
    }

    private List<ApiRole> listRoleByUserId(Long userId) {
        MPJLambdaWrapper<SysRole> joinWrapper = JoinWrappers.lambda();
        joinWrapper.leftJoin(SysUserRole.class, SysUserRole::getRoleId, SysRole::getRoleId);
        joinWrapper.eq(SysUserRole::getUserId, userId);
        return sysRoleDao.selectJoinList(ApiRole.class, joinWrapper);
    }
}
